<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible"
  >
    <el-form
      ref="dataForm"
      :model="dataForm"
      :rules="dataRule"
      label-width="140px"
      @keyup.enter.native="dataFormSubmit()"
    >
      <el-form-item label="受试者编号" prop="testeeNo">
        <el-input v-model="dataForm.testeeNo"  placeholder="请输入受试者编号"/>
      </el-form-item>
      <el-form-item label="姓名" prop="name">
        <el-input v-model="dataForm.name"  placeholder="请输入受试者姓名"/>
      </el-form-item>
      <el-form-item label="性别" prop="sex">
        <el-select
          v-model="dataForm.sex"
          placeholder="请选择受试者性别"
          style="width:100%"
        >
          <el-option
            v-for="option in roleOptions"
            :key="option.sexValue"
            :label="option.sexName"
            :value="option.sexValue"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="出生日期" prop="birthday">
        <el-date-picker
          v-model="dataForm.birthday"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择受试者出生日期">
        </el-date-picker>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import subject from '@/api/subject'
import research from '@/api/research'
import site from '@/api/site'
import myUtils from '../../api/myUtils'


export default {
  data() {
    return {
      visible: true,
      dataRule: {
        testeeNo: [{ required: true, message: '受试者编号不能为空', trigger: 'blur' }],
        name: [{ required: true, message: '姓名不能为空', trigger: 'blur' }],
        sex: [{ required: true, message: '性别不能为空', trigger: 'blur' }],
        birthday: [{ required: true, message: '生日不能为空', trigger: 'blur' }],
      },
      dataForm: {
        id: 0
      },
      inputVisible: false,
      inputValue: '',
      roleOptions: [{
        sexValue: 1,
        sexName: '男'
      }, {
        sexValue: 0,
        sexName: '女'
      }],
    }
  },
  methods: {
    init(id) {
      console.log(id)
      this.dataForm = {}
      this.visible = true
      this.$refs['dataForm'].resetFields()
      if (id != 0) {
        console.log("获取数据")
        subject.getById(id).then((response) => {
          if (response.code == '000000') {
            this.dataForm = response.data
          } else {
            this.$message.error('获取数据失败')
          }
        })
      }
      this.dataForm.id = id || 0 // 这一句其实可有可无的
      // this.dataForm.createPin = this.$store.getters.name
    },
    dataFormSubmit() {
      console.log(this.dataForm)
      if(this.dataForm.type === 0 && this.dataForm.id !== 0){
        this.$message.error("同步数据无法编辑");
        return;
      }
      console.log("提交")
      this.$refs['dataForm'].validate(valid => {
        if (valid) {
          if (this.dataForm.id === 0) {
            const timestamp = Date.now();
            const randomDigits = Math.floor(Math.random() * 900) + 100;
            this.dataForm.testeeVo = timestamp + randomDigits;
            this.dataForm.type = 1
            this.dataForm.subjectPkId = this.$route.query.subjectPkId
            console.log(this.dataForm.subjectPkId)
            research.getBysubjectPkId(this.dataForm.subjectPkId).then(response => {
              console.log(response)
              this.dataForm.subjectCode = response.data.subjectCode

            this.dataForm.sitePkId = myUtils.getSite(this.$route.query.subjectPkId,this.$store.getters.roles);
            site.getSiteBysitePkId(this.dataForm.sitePkId).then(response2 => {
              console.log(response2)
              this.dataForm.siteCode = response2.data.siteCode

            console.log(this.dataForm)
            subject.addTestee(this.dataForm).then(({ code }) => {
              if (code === '000000') {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 700,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
            })
            })
          } else {
            this.dataForm.type = 1
            console.log(this.dataForm)
            subject.updateTestee(this.dataForm).then(({ code }) => {
              if (code === '000000') {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 700,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        }
      })
    },
  }
}
</script>

<style scoped>
</style>
